package bpm.dao.impl.deptagenda;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import bpm.dao.deptagenda.AgendaDeptDao;
import bpm.domain.deptagenda.AgendaDept;
/**
 * 
 * @author wanglong635@126.com
 *
 */
@Repository("agendaDeptDao")
public class AgendaDeptImpl implements AgendaDeptDao {
	@javax.annotation.Resource private JdbcTemplate jdbcTemplate;
	
	public void update(String sql, Object... objs) {
		this.jdbcTemplate.update(sql, objs);
	}

	@SuppressWarnings("unchecked")
	public List<AgendaDept> findList(String sql, Object... objs) {

		return this.jdbcTemplate.query(sql, objs,new RowMapper(){
			public Object mapRow(ResultSet rs, int i) throws SQLException {
				AgendaDept dept = new AgendaDept();
				dept.setDeptCode(rs.getString("DEPTCODE"));
				dept.setDeptName(rs.getString("DEPTNAME"));
				dept.setDeptIndex(rs.getString("DEPTINDEX"));
				dept.setDeptParent(new AgendaDept(
						rs.getString("DEPTPCODE"),
						rs.getString("DEPTPNAME"),
						rs.getString("DEPTPINDEX")
				));
				
				Timestamp deptCtime = rs.getTimestamp("DEPTCTIME");
				if(deptCtime!=null)
					dept.setDeptCtime(String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", deptCtime));
					
				Timestamp deptUtime = rs.getTimestamp("DEPTUTIME");
				if(deptUtime!=null)
					dept.setDeptUtime(String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", deptUtime));
				
				
				return dept;
			}});
	}
}
